/**
 * author irlab.whu
 * 2010-12-19
 */
package org.whuims.pois.dao;

import org.whuims.pois.action.result.DataVo;
import org.whuims.pois.hibernate.BaseDao;
import org.whuims.pois.model.Stare;

import java.util.List;


/**
 * Ajax处理关注的逻辑方法
 *
 * @author irlab.whu
 *         <p/>
 *         2010-12-19
 */
public class StarDao {
    BaseDao baseDao = new BaseDao();

    /**
     * 所有关注列表
     *
     * @param page
     * @param size
     * @param userName
     * @param topicId
     * @return
     */
    public List<DataVo> list(int page, int size, String userName, long topicId) {
        List<DataVo> result = baseDao.queryByParamArr(page, size,
                "select new org.whuims.pois.action.result.DataVo(s.dataid,d.titlecontent, d.titlelink, d.abstext, d.sitename, d.newstime, d.topicid, d.crawltime) " +
                        "from DataC d,Stare s " +
                        "where d.id=s.dataid and s.user=? and d.topicid=?", userName, topicId
        );
        return result;
    }

    public boolean add(long id, String userName) {
        Stare stare = new Stare(userName, id);
        baseDao.create(stare);
        return true;
    }

    public boolean delete(long dataId, String userName) {
        List<Stare> list = baseDao.findByParamsArr("from Stare where dataid =? and user=?", dataId, userName);
        if (list == null || list.size() == 0) {
            return true;
        } else {
            baseDao.delete(list.get(0));
            return true;
        }
    }

}
